package com.mingsoft.cms.action.web;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.mingsoft.base.filter.DateValueFilter;
import com.mingsoft.base.filter.DoubleValueFilter;
import com.mingsoft.basic.action.BaseAction;
import com.mingsoft.basic.biz.IColumnBiz;
import com.mingsoft.basic.entity.CategoryEntity;
import com.mingsoft.basic.entity.ColumnEntity;
import com.mingsoft.cms.biz.IBasePropertyBiz;
import com.mingsoft.cms.biz.ICompanyBiz;
import com.mingsoft.cms.biz.IInsuranceBiz;
import com.mingsoft.cms.biz.IPropertyValueBiz;
import com.mingsoft.cms.constant.ModelCode;
import com.mingsoft.cms.entity.BasePropertyEntity;
import com.mingsoft.cms.entity.CompanyEntity;
import com.mingsoft.cms.entity.InsuranceEntity;
import com.mingsoft.cms.entity.PropertyValueEntity;
import com.mingsoft.cms.entity.InsuranceEntity;
import com.mingsoft.util.JsonUtil;
import com.mingsoft.util.StringUtil;

import net.mingsoft.base.util.JSONObject;
import net.mingsoft.basic.bean.EUListBean;
import net.mingsoft.basic.util.BasicUtil;

@Controller("insViewAction")
@RequestMapping("/ins/view")
public class InsViewAction extends BaseAction{
    /**
     * 注入博客文章业务层
     */    
	 @Autowired
    private IInsuranceBiz insuranceBiz;
    
    @Autowired
    private ICompanyBiz companyBiz;
    
    
    @Autowired
    private IBasePropertyBiz basePropertyBiz;
    
    @Autowired
    private IPropertyValueBiz propertyValueBiz;
  
	/**
	 * 栏目管理业务处理层
	 */
	@Autowired
	private IColumnBiz columnBiz;
    /**
     * 返回主界面index
     */
    @RequestMapping("/index")
    public String index(HttpServletResponse response,HttpServletRequest request,ModelMap model){
//      BasicUtil.startPage();
    	//所有公司
    	List<CompanyEntity>  companyList = companyBiz.queryAll();
    	model.addAttribute("companyList", companyList);
       //属性 年限
       PropertyValueEntity pv = new PropertyValueEntity();
       pv.setPropertyId(2);
       List<PropertyValueEntity> yearList = propertyValueBiz.query(pv);
       model.addAttribute("yearList", yearList);
       
       //属性 人群
       pv.setPropertyId(8);
       List<PropertyValueEntity> peoList = propertyValueBiz.query(pv);
       model.addAttribute("peoList", peoList);
       //属性 额度
       pv.setPropertyId(9);
       List<PropertyValueEntity> limitList = propertyValueBiz.query(pv);
       model.addAttribute("limitList", limitList);
       
       int pageSize = 6;
       int page = 1;
       String pagaIndex = request.getParameter("pageIndex");
       if(StringUtils.isNotBlank(pagaIndex)){
    	   page = Integer.valueOf(pagaIndex);
       }
       int startPage = (page-1) * pageSize;
       int endPage = page * pageSize;
       
     
       
       String insSql = " select fs.ins_id,fs.title,fs.small_image,fs.start_price,fs.synopsis,"
       				+ " ic.company_name,ic.company_logo  "
       				+ "  from t_fs_insurance fs "
       				+ "  left join t_fs_insurance_company  ic on fs.company_id = ic.company_id"
       				+ "   where 1=1  and del <> 1 ";
       String insType = request.getParameter("ins_type");
       String ids ="";
       String countSql = " select count(1) count from t_fs_insurance fs where 1=1  and del <> 1 ";
       
       //品类过滤
       if(StringUtils.isNotBlank(insType)){
    	   Integer curColuminId = Integer.valueOf(insType);
    	   int appId = this.getAppId(request);
    	   int[] _columnIds = columnBiz.queryChildrenCategoryIds(curColuminId, appId,0);
    	   
    	   for (int i : _columnIds) {
    		   ids = ids + i + ",";
    	   }
    	   if(ids.length() > 0 ){
    		   ids = ids.substring(0, ids.length()-1);
    		   insSql 	+= " and fs.ins_type in (" + ids + ")";
    		   countSql += " and fs.ins_type in (" + ids + ")";
    	   }
       }
       //公司过滤
       String company = request.getParameter("company");
       if(StringUtils.isNotBlank(company) && !"-1".equals(company) ){
    	   insSql 	+= " and fs.company_id  = " + company ;
		   countSql += " and fs.company_id  = " + company;
       }
       //期限过滤 人群  年限
       String year =  request.getParameter("year");
       String peo =  request.getParameter("peo");
       String lim =  request.getParameter("lim");
       String pram = "";
       if(StringUtils.isNotBlank(year) && !"-1".equals(year)){
    	   insSql 	+= " and fs.ins_id in  (select ins_id from t_fs_insurance_property where bpv_id   =" + year + ") "  ;
		   countSql += " and fs.ins_id in   (select ins_id from t_fs_insurance_property where bpv_id  =" + year + ") "  ;
       }
       if(StringUtils.isNotBlank(peo) && !"-1".equals(peo)){
    	   insSql 	+= " and fs.ins_id in  (select ins_id from t_fs_insurance_property where bpv_id =" + peo + ") "  ;
		   countSql += " and fs.ins_id in   (select ins_id from t_fs_insurance_property where bpv_id  =" + peo + ") "  ;
       }
       
       if(StringUtils.isNotBlank(lim) && !"-1".equals(lim)){
    	   insSql 	+= " and fs.ins_id in  (select ins_id from t_fs_insurance_property where bpv_id =" + lim + ") "  ;
		   countSql += " and fs.ins_id in   (select ins_id from t_fs_insurance_property where bpv_id  =" + lim + ") "  ;
       }
       
       
//       if(StringUtils.isNotBlank(pram) ){
//    	   pram = pram.substring(0, pram.length() -1);
//    	   insSql 	+= " and fs.ins_id in  (select ins_id from t_fs_insurance_property where bpv_id in (" + pram + ")) "  ;
//		   countSql += " and fs.ins_id in   (select ins_id from t_fs_insurance_property where bpv_id in (" + pram + ")) "  ;
//       }
       
       String insListSql 	=  insSql + "   order by fs.create_date desc "
       						+ "  limit " + startPage + "," + endPage;
      
      
       List<Map> totalList = (List<Map>) insuranceBiz.excuteSql(countSql);
       if( !totalList.isEmpty() ){
    	   Map totalMap = totalList.get(0);
    	   model.addAttribute("total",totalMap.get("count"));
    	   model.addAttribute("currentPage",page);
       }
     
     
       List<Map<String,Object>>  insList = (List<Map<String, Object>>) insuranceBiz.excuteSql(insListSql);
       model.addAttribute("insList", insList);
       
    	return view ("/goods/view/products_list");
    }
    

    /**
     * 返回主界面index
     */
    @RequestMapping("/detail")
    public String detail(HttpServletResponse response,HttpServletRequest request,ModelMap model){
//     
    	String insId = request.getParameter("insId");
    	InsuranceEntity ins = new InsuranceEntity(); 
    	if(StringUtils.isNotBlank(insId)){
    		InsuranceEntity insTemp   =  (InsuranceEntity) insuranceBiz.getEntity(Integer.valueOf(insId));
    		if( null != insTemp){
    			ins = insTemp;
    		}
    	}
    	
    	model.addAttribute("ins",ins);
    	
    	//带出商品属性
    	String bpSql = " select fs.ip_id,bp.bp_id,bp.property_name from  t_fs_insurance_property fs "
    			+ " left join t_fs_base_property bp on bp.bp_id = fs.bp_id "
    			//+ " left join t_fs_property_value pv on pv.pv_id = fs.bpv_id "
    			+ "where fs.ins_id = " + insId;
    	List<Map<String,Object>> bpList = (List<Map<String,Object>>) insuranceBiz.excuteSql(bpSql);
    	for (Map<String, Object> map : bpList) {
			
    		
		}
    	
    	
    	return view ("/goods/view/prodcut_detail");
    }
    
    /**====================手机端========================*/

    /**
     * 返回主界面index
     */
    @RequestMapping("/indexM")
    public String indexM(HttpServletResponse respone,HttpServletRequest request,ModelMap model){
//      BasicUtil.startPage();
    	//所有公司
    	List<CompanyEntity>  companyList = companyBiz.queryAll();
    	model.addAttribute("companyList", companyList);
    	
    	//所有保险种类
    	List<Map<String,Object>> insPageList = new ArrayList<Map<String,Object>>();
    	String insTypeSql = " select  category_id id, category_title label from  category "
    			+ "   where category_parent_id like '149,%' and length(category_parent_id) < 9";
    	List<Map<String,Object>> instypeList = (List<Map<String, Object>>) insuranceBiz.excuteSql(insTypeSql);
    	Map allMap = new HashMap<String,Object>();
    	allMap.put("id", "-1");
    	allMap.put("label", "全部");
    	insPageList.add(allMap);
    	insPageList.addAll(instypeList);
    	
    	String tableId = request.getParameter("tableId");
    	if(StringUtils.isBlank(tableId)){
    		tableId = "-1";
    	}
    	
    	model.addAttribute("tableId",tableId);
    	model.addAttribute("insTypeList",  JSONArray.toJSONString(insPageList));
    	
       //年限
       PropertyValueEntity pv = new PropertyValueEntity();
       pv.setPropertyId(2);
       List<PropertyValueEntity> peoList = propertyValueBiz.query(pv);
       model.addAttribute("peoList", peoList);
       
       
    	return view ("/goods/view/products_list_m");
    }
   
    @RequestMapping("/getInsList")
    @ResponseBody
    public void get(@ModelAttribute InsuranceEntity insurance,HttpServletResponse response, HttpServletRequest request,ModelMap model){
    	int pageSize = 10;
        int page = 1;
        String pageNo = request.getParameter("pageNo");
        if(StringUtils.isNotBlank(pageNo)){
     	   page = Integer.valueOf(pageNo);
        }
        int startPage = (page-1) * pageSize;
        int endPage = page * pageSize;
      
        
        String insSql = " select fs.ins_id insId,fs.title,fs.small_image img,fs.start_price price,fs.synopsis,"
        				+ " ic.company_name,ic.company_logo  "
        				+ "  from t_fs_insurance fs "
        				+ "  left join t_fs_insurance_company  ic on fs.company_id = ic.company_id"
        				+ "   where 1=1  and del<> 1";
        String insType = request.getParameter("tabId");
        String ids ="";
        String countSql = " select count(1) count from t_fs_insurance fs where 1=1 ";
        if(StringUtils.isNotBlank(insType) && !"-1".equals(insType)){
     	   Integer curColuminId = Integer.valueOf(insType);
     	   int appId = this.getAppId(request);
     	   int[] _columnIds = columnBiz.queryChildrenCategoryIds(curColuminId, appId,0);
     	   
     	   for (int i : _columnIds) {
     		   ids = ids + i + ",";
     	   }
     	   if(ids.length() > 0 ){
     		   ids = ids.substring(0, ids.length()-1);
     		   insSql 	+= " and fs.ins_type in (" + ids + ")";
     		   countSql += " and fs.ins_type in (" + ids + ")";
     	   }
        }
        
        //年限过滤
        String year =  request.getParameter("ageId");
        if(StringUtils.isNotBlank(year) && !"0".equals(year)){
     	   insSql 	+= " and fs.ins_id in  (select ins_id from t_fs_insurance_property where bpv_id   =" + year + ") "  ;
 		   countSql += " and fs.ins_id in   (select ins_id from t_fs_insurance_property where bpv_id  =" + year + ") "  ;
        }
        
        //公司过滤
        String company = request.getParameter("brandId");
        if(StringUtils.isNotBlank(company) && !"0".equals(company) ){
     	   insSql 	+= " and fs.company_id  = " + company ;
 		   countSql += " and fs.company_id  = " + company;
        }
        
        String insListSql 	=  insSql + "   order by fs.create_date desc "
        						+ "  limit " + startPage + "," + endPage;
       
       
        List<Map> totalList = (List<Map>) insuranceBiz.excuteSql(countSql);
        if( !totalList.isEmpty() ){
     	   Map totalMap = totalList.get(0);
     	   model.addAttribute("total",totalMap.get("count"));
     	   model.addAttribute("currentPage",page);
        }
      
      
        List<Map<String,Object>>  insList = (List<Map<String, Object>>) insuranceBiz.excuteSql(insListSql);
        model.addAttribute("insList", insList);
        this.outJson(response, insList);
    }
    
    /**
     * 返回主界面index
     */
    @RequestMapping("/detailM")
    public String detailM(HttpServletResponse response,HttpServletRequest request,ModelMap model){
//     
    	String insId = request.getParameter("insId");
    	InsuranceEntity ins = new InsuranceEntity(); 
    	if(StringUtils.isNotBlank(insId)){
    		InsuranceEntity insTemp   =  (InsuranceEntity) insuranceBiz.getEntity(Integer.valueOf(insId));
    		if( null != insTemp){
    			ins = insTemp;
    		}
    	}
    	
    	model.addAttribute("ins",ins);
    	
    	//带出商品属性
    	String bpSql = " select fs.ip_id,bp.bp_id,bp.property_name from  t_fs_insurance_property fs "
    			+ " left join t_fs_base_property bp on bp.bp_id = fs.bp_id "
    			//+ " left join t_fs_property_value pv on pv.pv_id = fs.bpv_id "
    			+ "where fs.ins_id = " + insId;
    	List<Map<String,Object>> bpList = (List<Map<String,Object>>) insuranceBiz.excuteSql(bpSql);
    	for (Map<String, Object> map : bpList) {
			
    		
		}
    	
    	
    	return view ("/goods/view/prodcut_detail_m");
    }
    
}